package com.scf.fruit.shiro;

import com.scf.fruit.user.FruitUserDto;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;

import javax.annotation.Resource;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @date : 2018-04-23 10:01
 */

public class MyShiroRealm extends AuthorizingRealm {
    @Resource
    private FruitUserService fruitUserService;
    @Resource
    private RoleRelationService roleRelationService;
    @Resource
    private RoleService roleService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private PermissionRelationService permissionRelationService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取登录用户名
        String mobile= (String) principalCollection.getPrimaryPrincipal();
        //查询用户名称
        FruitUserDto byMobile = fruitUserService.findByMobile(mobile);
        RoleRelationDto dto=new RoleRelationDto();
        dto.setUserId(byMobile.getId());
        List<RoleRelationDto> roleRelationDtos = roleRelationService.queryList(dto);
        Set<Long> collect = roleRelationDtos.stream().map(x -> x.getRoleId()).collect(Collectors.toSet());
        List<RoleDto> roleDtos = roleService.queryRoleByList(collect);
        List<PermissionRelationDto> permissionRelationDtos = permissionRelationService.queryRelationByList(collect);
        Set<Long> permissionIds=permissionRelationDtos.stream().map(x->x.getPermissionId()).collect(Collectors.toSet());
        List<PermissionDto> permissionDtos = permissionService.queryPermissionByList(permissionIds);
        Map<Long, List<PermissionRelationDto>> map = permissionRelationDtos.stream().collect(Collectors.groupingBy(x -> x.getRoleId()));
        Map<Long, PermissionDto> perMap = permissionDtos.stream().collect(Collectors.toMap(x -> x.getId(), y -> y));
        //添加角色和权限
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (RoleDto role:roleDtos) {
            //添加角色
            simpleAuthorizationInfo.addRole(role.getRoleName());
            for (PermissionRelationDto permission:map.get(role.getId())) {
                //添加权限
                simpleAuthorizationInfo.addStringPermission(perMap.get(perMap.get(permission.getPermissionId())).getPermission());
            }
        }
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //加这一步的目的是在Post请求的时候会先进认证，然后在到请求
        if (authenticationToken.getPrincipal() == null) {
            return null;
        }
        //获取用户信息
        String mobile = authenticationToken.getPrincipal().toString();
        FruitUserDto byMobile = fruitUserService.findByMobile(mobile);
        if (byMobile == null) {
            //这里返回后会报出对应异常
            return null;
        } else {
            //这里验证authenticationToken和simpleAuthenticationInfo的信息
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(mobile, Base64.getEncoder().encodeToString(byMobile.getPassword()), getName());


            AdminUserUtil.setCurrentUser(byMobile);
            return simpleAuthenticationInfo;
        }
    }
}
